package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.controller.form.StateForm;
import com.woniuxy.controller.info.*;
import com.woniuxy.dao.UserDao;
import com.woniuxy.model.Defectstate;
import com.woniuxy.dao.DefectstateDao;
import com.woniuxy.model.Defecttype;
import com.woniuxy.model.User;
import com.woniuxy.service.DefectstateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class DefectstateServiceImpl extends ServiceImpl<DefectstateDao, Defectstate> implements DefectstateService {
    @Resource
    private DefectstateDao defectstateDao;
    @Resource
    private UserDao userDao;
    @Override
    public boolean change(StateForm stateForm) {
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account_id", userInfo.getId()));
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        Defectstate defectstate = new Defectstate();
        defectstate.setDefectstateId(stateForm.getDefectstateId());
        defectstate.setDefectstateNum(stateForm.getDefectstateNum());
        defectstate.setDefectstateName(stateForm.getDefectstateName());
        defectstate.setDefectstateChangenameId(user.getUId());
        defectstate.setDefectstateChangetime(nowTimeToString);
        defectstate.setDefectstateChangename(user.getUName());
        defectstate.setDefectstateSign(stateForm.getDefectstateSign());
        int update = defectstateDao.updateById(defectstate);
        if (update>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<StateInfo> findAll() {
        ArrayList<StateInfo> stateInfos = new ArrayList<>();
        List<Defectstate> defectstates = defectstateDao.selectAll();


        for (Defectstate defectstate:defectstates){
            StateInfo stateInfo = CopyUtil.copyOne(defectstate, StateInfo.class);
            if (defectstate.getDefectstateState()==1){
                stateInfo.setDefectstateNewState("有效");
            }else{
                stateInfo.setDefectstateNewState("禁用");
            }
            stateInfos.add(stateInfo);
        }

        return stateInfos;
    }

    @Override
    public List<DefectStateIdAndState> getdefectStateIsOk() {
        List<DefectStateIdAndState> defectStateIdAndStates = defectstateDao.selectStateIsOk();

        return defectStateIdAndStates;
    }

    @Override
    public Integer add(StateForm stateForm) {
        stateForm.setDefectstateState(1);
        Defectstate defectstate = CopyUtil.copyOne(stateForm, Defectstate.class);
        int insert= defectstateDao.insert(defectstate);
        return insert;

    }

    @Override
    public Integer delectByIds(List<Integer> ids) {
        int i = this.baseMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public Integer changeState(StateForm stateForm) {
        String state = stateForm.getDefectstateNewState();

        if(state.equals("有效")){
            stateForm.setDefectstateState(0);
        }else {
            stateForm.setDefectstateState(1);
        }
        Defectstate defectstate = CopyUtil.copyOne(stateForm, Defectstate.class);
        UpdateWrapper<Defectstate> wrapper = new UpdateWrapper<>();
        wrapper.eq("defectState_name",defectstate.getDefectstateName()).set("defectState_state",defectstate.getDefectstateState());
        int update = defectstateDao.update(null, wrapper);
        return update;
    }

    @Override
    public List<DefectStateIdAndState> getDefectNoInitState() {

        List<DefectStateIdAndState> defectStateIdAndStates = this.baseMapper.selectNoInitState();

        return defectStateIdAndStates;
    }

    @Override
    public List<StateInfo> selectByState(String defectstateSign) {
        ArrayList<StateInfo> stateInfos = new ArrayList<>();
        QueryWrapper<Defectstate> wrapper = new QueryWrapper<>();
        wrapper.eq("defectState_sign",defectstateSign);
        List<Defectstate> defectstateList = this.baseMapper.selectList(wrapper);
        for (Defectstate defectstate : defectstateList) {
            StateInfo stateInfo = CopyUtil.copyOne(defectstate, StateInfo.class);
            if (defectstate.getDefectstateState()==1){
                stateInfo.setDefectstateNewState("有效");
            }else{
                stateInfo.setDefectstateNewState("禁用");
            }
            stateInfos.add(stateInfo);
        }
        return stateInfos;
        }




    @Override
    public List<DefectstateNamesInfo> getChoiceStates() {
        List<DefectstateNamesInfo> defectstateNamesInfos=this.baseMapper.selectChoiceStates();
        return defectstateNamesInfos;
    }
}
